﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Utilities;

namespace StockCommon.data
{
    public class StockDataModule
    {

        public class  WeekStartEnd
        {
            public int YearWeekKey { get; set; }
            public DateTime startDay { get; set; }
            public DateTime endDay { get; set; }
        }

        protected static List<int> yearWeekKeys = new List<int>();
        protected static Dictionary<int,WeekStartEnd>  weekToDay = null;

        public static void getMinMaxDate(String code, out DateTime minDay, out DateTime maxDay)
        {
            StockDataClassesDataContext context = getContext();
            if (String.IsNullOrWhiteSpace(code))
            {
                var maxday = (from sd in context.StockDailies  select sd.日期).Max();
                var minday = (from sd in context.StockDailies  select sd.日期).Min();
                minDay = (DateTime)minday;
                maxDay = (DateTime)maxday;
            }
            else
            {
                var maxday = (from sd in context.StockDailies where sd.證券代號 == code select sd.日期).Max();
                var minday = (from sd in context.StockDailies where sd.證券代號 == code select sd.日期).Min();
                minDay = (DateTime)minday;
                maxDay = (DateTime)maxday;
            }
        }
        public static int getOffsetYearWeekKey(int yearWeekKey, int offset)
        {
            int pos = yearWeekKeys.IndexOf(yearWeekKey);
            pos = pos + offset;
            if (pos < 0)
                pos = 0;
            if (pos >= yearWeekKeys.Count)
                pos = yearWeekKeys.Count - 1;
            return yearWeekKeys[pos];
        }

        public static Dictionary<int, WeekStartEnd> getWeekToDay()
        {
            
            if (weekToDay == null)
                initWeekToDay();
            return weekToDay;
        }
        protected static void initWeekToDay()
        {
            StockDataClassesDataContext context = getContext();
            String sql ="select YearWeekKey,MIN([date]) as startDay,MAX([date]) as endDay from TradeCalendar group by TradeCalendar.YearWeekKey";
            var data=context.ExecuteQuery<WeekStartEnd>(sql);
            weekToDay = new Dictionary<int, WeekStartEnd>();
            foreach (WeekStartEnd d in data)
            {
                weekToDay.Add(d.YearWeekKey, d);
                yearWeekKeys.Add(d.YearWeekKey);
            }
        }

        private static StockDataClassesDataContext  getContext()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            return context;
        }
        public static DateTime getStartDay(int yearWeek)
        {
            if (weekToDay == null)
                initWeekToDay();
            return ((WeekStartEnd)weekToDay[yearWeek]).startDay;

        }
        public static DateTime getEndDay(int yearWeek)
        {
            if (weekToDay == null)
                initWeekToDay();
            return ((WeekStartEnd)weekToDay[yearWeek]).endDay;
        }

        public static List<StockDaily> getDailyIndexAndVolume(String code, int dayCount)
        {
            if (dayCount <= 0)
                    dayCount = 600;
            String sql=String.Format("select * from (select top {0} *  from StockDaily where StockDaily.證券代號='{1}' order by StockDaily.日期 desc) t order by 日期 asc ", dayCount, code);
            StockDataClassesDataContext context = LinqAgent.getContext();
            List<StockDaily> result= context.ExecuteQuery<StockDaily>(sql).ToList<StockDaily>();
            return result;
        }
        public static List<WeekIndexAndVolume> getWeekIndexAndVolume(String code)
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            String sql = string.Format("select   *  from WeekIndexAndVolumes where stockCode ={0} and WeekIndexAndVolumes.開盤價 is not null order by  YearWeekKey", code);
            List<WeekIndexAndVolume> ll = context.ExecuteQuery<WeekIndexAndVolume>(sql).ToList();
            return ll;
        }
        public static double get前日收盤價(String code, DateTime dt)
        {
            String sql = String.Format("select top 1 StockDaily.收盤價 from StockDaily where StockDaily.證券代號='{0}' and StockDaily.日期 < '{1}' order by StockDaily.日期 desc ", code, DateUtils.getPlanString(dt));
            StockDataClassesDataContext context = LinqAgent.getContext();
            decimal? v=context.ExecuteQuery<decimal>(sql).FirstOrDefault<decimal>();
            if (v == null)
                return 0;
            return Convert.ToDouble(v);
        }

    }
}
